package com.launch.wlfw.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.launch.wlfw.dao.LendMapper;
import com.launch.wlfw.dto.LendListDTO;
import com.launch.wlfw.pojo.BookInfo;
import com.launch.wlfw.pojo.LendList;
import com.launch.wlfw.pojo.ReaderInfo;
import com.launch.wlfw.service.LendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class LendServiceImpl implements LendService {

    @Resource
    private LendMapper lendMapper;


    @Override
    public PageInfo<LendListDTO> selectByParams(Map<String, String> params) {
        String page = params.getOrDefault("page", "0");
        String limit = params.getOrDefault("limit", "15");
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));

        return PageInfo.of(lendMapper.selectByParams(params));
    }


    @Override
    public int addLend(String readerNumber, List<Integer> ids) {
        List<LendList> lends = new ArrayList<>();
        Map<String, String> map = new HashMap<>();

        ReaderInfo reader = lendMapper.getReaderInfoByReaderNumber(readerNumber);
        if (reader == null) {
            return 0;
        }
        for (Integer id : ids) {
            LendList lendList = new LendList();
            Date date = new Date();
            map.put("id", String.valueOf(id));
            map.put("status", "1");
            lendMapper.updateBookInfo(map);

            lendList.setBookId(id);
            lendList.setReaderId(reader.getId());
            lendList.setLendDate(date);
            //BackType==4 已被借
            lendList.setBackType(4);

            lends.add(lendList);
        }
        return lendMapper.addLend(lends);
    }

    @Override
    public int deleteById(List<Integer> ids, List<Integer> bookIds) {
        Map<String, String> map = new HashMap<>();
        for (Integer bookId : bookIds) {
            map.put("id", String.valueOf(bookId));
            map.put("status", "0");
            lendMapper.updateBookInfo(map);
        }
        return lendMapper.deleteById(ids);
    }

    @Override
    public PageInfo<BookInfo> getAllBook(Map<String, String> params) {
        String page = params.getOrDefault("page", "0");
        String limit = params.getOrDefault("limit", "15");
        params.put("status", "0");
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        return PageInfo.of(lendMapper.getAllBook(params));
    }

    @Override
    public int backLendListByIds(List<Integer> ids, List<Integer> bookIds) {
        Map<String, String> map = new HashMap<>();
        for (Integer bookId : bookIds) {
            map.put("id", String.valueOf(bookId));
            // status == 0 未借出
            map.put("status", "0");
            lendMapper.updateBookInfo(map);
        }

        for (Integer id : ids) {
            LendList lendList = new LendList();
            lendList.setId(id);
            // backType == 0 正常还书
            lendList.setBackType(0);
            lendList.setBackDate(new Date());
            lendMapper.updateLendList(lendList);
        }
        return 1;
    }

    @Override
    public int updateLendInfoSubmit(Map<String, String> params) {
        HashMap<String, String> map = new HashMap<>();
        map.put("id", params.get("bookId"));
        if ("3".equals(params.get("backType"))) {
            // backType==3 丢失
            // 设status=2 为书本丢失
            map.put("status", "2");
        }else {
            //status=0 为书本归还
            map.put("status", "0");
        }
        lendMapper.updateBookInfo(map);


        LendList lendList = new LendList();
        lendList.setId(Integer.valueOf(params.get("id")));
        lendList.setExceptRemarks(params.get("remarks"));
        lendList.setBackType(Integer.valueOf(params.get("backType")));
        // backType==1 延迟还书
        if("1".equals(params.get("backType")) || "3".equals(params.get("backType"))){

        }else {
            lendList.setBackDate(new Date());
        }
        lendMapper.updateLendList(lendList);
        return 0;
    }

    @Override
    public List<LendListDTO> queryLookBookList(Map<String,String> params) {
        HashMap<String, String> map = new HashMap<>();
        if("user".equals(params.get("flag"))){
            map.put("readerId",params.get("id"));
        }else if("book".equals(params.get("flag"))){
            map.put("bookId",params.get("id"));
        }else if(params.get("readerId")!=null){
            map.put("readerId", params.get("readerId"));
        }

        log.info("__________________queryLookBookList map="+map.get("readerId"));

        List<LendListDTO> lendListDTOS = lendMapper.selectByParams(map);
        return lendListDTOS;
    }
}
